﻿using System;
using TheGameClient.Processes;
using TheGameClient.Webservices;
using Waaahsabi.Controls.TheGame;

namespace TheGameClient.SoftwareUI.FeatureUI
{
	public partial class FeatureLocalChatControl : ISystemFeature
	{
		public FeatureLocalChatControl()
		{
			InitializeComponent();
			ProcessId = Guid.NewGuid();
			App.Client.LocalChatMessageReceived += ClientLocalChatMessageReceived;
			chatControl.SendMessage += SendMessage;
		}

		#region ISystemFeature Members

		public ulong InterfaceId
		{
			get { return new Guid("{AEE5CE77-E853-42a9-8857-15DA4837F4F9}").ToLong(); }
		}

		public Guid ProcessId { get; private set; }

		public string ProcessName
		{
			get { return "Local Chat"; }
		}

		public InterfaceCategory Category
		{
			get { return InterfaceCategory.SystemFeature; }
		}

		public ulong SystemId { get; private set; }

		public void Load(ulong systemId)
		{
			//nothing to "load" here. Its a chat.
			SystemId = systemId;
		}

		#endregion

		private void SendMessage(object sender, ChatControlMessageEventArgs e)
		{
			ChatMessage message = new ChatMessage {Message = e.Message, SystemId = SystemId};
			App.Client.SendChatMessageAsync(App.InternalSessionId, message);
		}

		private void ClientLocalChatMessageReceived(object sender, LocalChatMessageReceivedEventArgs e)
		{
			chatControl.ChatMessages.Add(
				new ChatMessageItem
				{
					MessageSender = "[ " + e.message.Sender + " ]",
					MessageText = e.message.Message,
					MessageTimestamp = e.message.Timestamp.ToShortTimeString()
				}
				);
		}
	}

	public class FeatureLocalChatUiFactory : UiFactoryBase
	{
		public override InterfaceCategory UiCategory
		{
			get { return InterfaceCategory.SystemFeature; }
		}

		public override IProcess CreateProgram()
		{
			return new FeatureLocalChatControl();
		}
	}
}